package com.project.communitymanagement.auth.common;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xgp
 * @version 1.0
 * @date 2020/5/13 13:26
 * @Annotate  登出
 */
@Log4j2
@Component
public class MyLogoutSuccessHandler  implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("退出成功");
        User user = (User) authentication.getPrincipal();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "onLogoutSuccess");
        jsonObject.put("message","onLogoutSuccess");
        jsonObject.put("result", user.getUsername() + "onLogoutSuccess");
       /* response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(jsonObject.toJSONString());*/
       response.sendRedirect("/login");
    }
}

